package com.our.sys.project1.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.our.sys.project1.dao.QueryDao;
import com.our.sys.project1.pojo.PriceReview;

@Service
@Transactional
public class QueryService {

    @Autowired
    private QueryDao queryDao;

    /**
     * 价格审核
     * @param price
     * @param startTime
     * @param endTime
     * @return
     * @throws Exception
     */
    @Transactional(readOnly = true)
    public List<PriceReview> getPriceReview(BigDecimal price, String startTime, String endTime,Page<PriceReview> page) throws Exception {
        StringBuffer sql = new StringBuffer(2000);
        List<Object> params = new ArrayList<Object>();

        sql.append("select a.time as discountDate,a.who as discountWho,b.number as number,d.price as discountPrice,");
        sql.append("c.readySendTime as useDate,f.name as useDepart,e.number as hNumber,h.price as price,");
        sql.append("e.name as person,d.managerCode as managerCode,g.name as supplier,d.factoryCode as factoryCode");
        sql.append(" from T_DiscountPrice a,T_RegAccount b,T_ReadySend c,T_ReadyCode d,B_Person e,B_Toffices f,B_Supplier g,B_HighValueMaterialDetail h ");
        sql.append("where 1=1");
        sql.append(" and a.readySendId = c.id and c.id = d.readySendId and d.id = b.readyCodeId and d.supplierId = g.id and d.discountId = a.id and ");
        sql.append("d.materialId = h.id and c.tofficesId = f.id and c.personId = e.id");
        sql.append(" and c.state = '2' and d.readyCodeState = '5'");
        if (null != price) {
            //sql.append(" and price");
            //params.add(price);
        }

        if (StringUtils.isNotBlank(startTime)) {
            sql.append(" and a.time >= ?");
            params.add(startTime);
        }

        if (StringUtils.isNotBlank(endTime)) {
            sql.append(" and a.time <= ?");
            params.add(endTime);
        }

        return queryDao.getPriceReview(sql.toString(), params);
    }
    
    /*public Long getPriceReviewCount(){
        
    }*/
}
